<?php
/**
 *
 * 管理后台接口都在这个控制器中
 * Created by PhpStorm.
 * User: summercode
 * Date: 2019/1/1
 * Time: 12:25 PM
 */

namespace app\index\controller;


use app\common\controller\BaseModel;
use app\common\model\AccontModel;
use app\common\model\OrderModel;
use app\common\model\RecodeModel;
use app\common\model\SellerInfoModel;
use app\common\model\SellerModel;
use app\common\model\SettingModel;
use app\common\model\TicketCheckModel;
use app\common\model\TicketModel;
use app\common\model\UserModel;
use think\Request;


/***
 * Class Account
 * @package app\index\controller
 *
 *     类下的方法
 *
 *     login 商户平台登录
 *     getActiveInfo 获取活动信息
 *     setActiveTime 设置活动时间
 *     getActiveTimeInfo
 *
 *     userList 用户列表接口
 *     checkDataList 核销列表接口
 *     addTicket 添加体验数
 *     addPrizeData 添加奖品
 *
 *     exportUserExcel 导出用户Excel
 *     exportSellerExcel 导出商品信息Excel
 *     exportCheckExcel 导出核销信息Excel
 *
 *     sellerInfoList 商户信息列表
 *     addSellerInfo 添加商户信息
 *     delSellerInfo 删除商户信息
 *
 *     sellerList  商户账号列表
 *
 */

class Account extends BaseModel
{
    // 构造方
    public function  __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin: *');
        parent::__construct($request);
    }

    // 管理后台登录
    public function login(Request $request) {
        $data = [
            'username' => $request->param('username'),
            'password' => $request->param('password'),
        ];

        if (empty($data['username']) || empty($data['password'])) {
            return $this->json_data(0, '000001', '请填写账号或者密码', '', '');
        }

        $Account = new AccontModel();
        $res = $Account->where('username', $data['username'])->value('password');
        $res_account = $Account->where('username', $data['username'])->find();

        if (empty($res_account)) {
            return $this->json_data(1, '100002', '账号不存在', '', '');
        }

        if ($res === $data['password']) {
            return $this->json_data(1, '000000', '登录成功', [
                'uid' => $res_account['id'],
                'username' => $res_account['username'],
                'role' => $res_account['role']
            ], '');
        } else {
            return $this->json_data(0, '100001', '密码错误', '', '');
        }
    }

    /**
     *  1、data1 活动用户数
     *  2、data2 活动抽奖礼包数
     *  3、data3 活动资金池
     *  4、data4 活动礼包数
     *
     */
    public function getActiveInfo(Request $request) {

        $userNum = new UserModel();
        $setting = new SettingModel();
        $order = new OrderModel();

        $data1 = count($userNum->select());

        $prize = $setting->where('jack_key', 'prizeArr')->value('jack_value');
        $prizeArr = explode(',', $prize);
        $data2 = count($prizeArr);

        $a = 0; // A5大奖
        $b = 0; //
        $c = 0;
        $d = 0;
        $e = 0;
        $f = 0;

        for ($i = 0; $i < $data2; $i++) {
            if (Floor($prizeArr[$i] / 1000) == 8) {
                ++$a;
            }
            if (Floor($prizeArr[$i] / 1000) == 5) {
                ++$b;
            }
            if (Floor($prizeArr[$i] / 1000) == 2) {
                ++$c;
            }
            if (Floor($prizeArr[$i] / 1000) == 7) {
                ++$d;
            }
            if (Floor($prizeArr[$i] / 1000) == 3) {
                ++$e;
            }
            if (Floor($prizeArr[$i] / 1000) == 6) {
                ++$f;
            }
        }

        $data3 = intval(count($order->select())) * 9.9;

        $data4 = $setting->where('jack_key', 'ticket')->value('jack_value');

        return $this->json_data(1, '000000', '活动信息', [
            'data1' => $data1,
            'data2' => $data2,
            'data3' => round($data3,2),
            'data4' => intval($data4),
            'recode' => [
                'a' => $a,
                'b' => $b,
                'c' => $c,
                'd' => $d,
                'e' => $e,
                'f' => $f,
            ]
        ], '');
    }


    // 设置活动时间
    public function setActiveTime(Request $request) {

        $startTime = $request->param('startTime');
        $endTime = $request->param('endTime');

        if (empty($startTime) || empty($endTime)) {
            return $this->json_data(0, '100001', '参数不完整', '', '');
        }

        $setting = new SettingModel();

        $setting->where('jack_key', 'start_time')->update([
            'jack_value' => $startTime
        ]);

        $setting->where('jack_key', 'end_time')->update([
            'jack_value' => $endTime
        ]);

        return $this->json_data(1, '000000', '设置活动时间段', '', '');
    }


    // 获取活动时间信息
    public function getActiveTimeInfo(Request $request) {
        $setting = new SettingModel();

        $start_time = $setting->where('jack_key', 'start_time')->value('jack_value');
        $end_time = $setting->where('jack_key', 'end_time')->value('jack_value');

        return $this->json_data(1, '000000', '', [
            'start_time' => $start_time,
            'end_time' => $end_time
        ], '');
    }


    // 用户展示接口
    public function userList(Request $request) {
        $page = $request->param('page') ? $request->param('page') : 1;
        $num = $request->param('num') ? $request->param('num') : 10;
        $type = $request->param('type') ? $request->param('type') : 0;
        $keyword = $request->param('keyword') ? $request->param('keyword') : '';

        $user = new UserModel();

        $data = null;
        $total = 0;

        if ($type == 0) {
            $data = $user->limit(($page - 1) * $num, $num)
                ->order('update_time', 'desc')
                ->select();

            $total = count($user->select());
        }

        if ($type == 1) {
            $data = $user->limit(($page - 1) * $num, $num)
                ->where('is_ticket', 1)
                ->order('update_time', 'desc')
                ->select();

            $total = count($user->where('is_ticket', 1)
                ->select());
        }

        if ($type == 2) {
            $data = $user->limit(($page - 1) * $num, $num)
                ->where('is_ticket', 0)
                ->order('update_time', 'desc')
                ->select();

            $total = count($user->where('is_ticket', 0)
                ->select());
        }

        if ($keyword) {
            $data = $user->limit(($page - 1) * $num, $num)
                ->whereOr('nickName', 'like', '%'.$keyword.'%')
                ->whereOr('phone', 'like', '%'.$keyword.'%')
                ->whereOr('name', 'like', '%'.$keyword.'%')
                ->order('update_time', 'desc')
                ->select();

            $total = count($user
                ->whereOr('nickName', 'like', '%'.$keyword.'%')
                ->whereOr('phone', 'like', '%'.$keyword.'%')
                ->whereOr('name', 'like', '%'.$keyword.'%')
                ->select());
        }


        return $this->json_data(1, '000000', '用户列表', [
            'total' => $total,
            'data' => $data
        ], '');
    }


    //核销记录

    public function checkDataList(Request $request) {
        $page = $request->param('page') ? $request->param('page') : 1;
        $num = $request->param('num') ? $request->param('num') : 10;
        $keyword = $request->param('keyword') ? $request->param('keyword') : '';


        $ticketCheck = new TicketCheckModel();
        $seller = new SellerModel();
        $sellerInfo = new SellerInfoModel();
        $user = new UserModel();
        $ticket = new TicketModel();


        $data = null;
        $total = 0;

        if (!empty($keyword)) {
            $uid = $user::where('phone' , $keyword)->value('id');

//            print($uid);

            $data = $ticketCheck
                ->where('uid', $uid)
                ->limit(($page - 1) * $num, $num)
                ->order('update_time', 'desc')
                ->select();

            $total = count($ticketCheck ->where('uid', $uid)->select());

        } else {

            $data = $ticketCheck->limit(($page - 1) * $num, $num)
                ->order('update_time', 'desc')
                ->select();

            $total = count($ticketCheck->select());

        }


        $result = array();

        foreach ($data as $item) {
            $userData = $user->where('id', $item['uid'])->find();
            $ticketData = $ticket->where('id', $item['qid'])->find();

            $phone = $seller->where('id', $item['sid'])->value('uname');
            $sellerData = $sellerInfo->whereOr('phone2', $phone)
                ->whereOr('phone1', $phone)
                ->find();
            $item['user1'] = $userData;
            $item['ticket1'] = $ticketData;
            $item['seller'] = $sellerData;

            array_push($result, $item);
        }

        return $this->json_data(1, '000000', '核销记录', [
            'total' => $total,
            'data' => $data
        ], '');

    }


    /**
     *
     * 添加优惠券名额
     * @param Request $request
     * @return \think\response\Json
     */
    public function addTicket(Request $request) {
        $num = $request->param('num');

        if ($num > 5000) {
            return $this->json_data(0, '800001', '数额较大，无法修改', '', '');
        }
        $setting = new SettingModel();
        $ticket_num = $setting->where('jack_key', 'ticket')->value('jack_value');

        $setting->where('jack_key', 'ticket')->update([
            'jack_value' => intval($ticket_num) + intval($num)
        ]);

        return $this->json_data(1, '000000', '修改成功', '', '');

    }


    /**
     *
     *
     * 添加奖品
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public function addPrizeData(Request $request)
    {
        $num = $request->param('num');
        $type = $request->param('type');

        $setting = new SettingModel();
        $prizeStr = $setting->where('jack_key', 'prizeArr')->value('jack_value');

        $prizeArr = explode(',', $prizeStr);

        if ($type == 1) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(1000, 1999));
            }
        }

        if ($type == 2) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(2000, 2999));
            }
        }

        if ($type == 3) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(3000, 3999));
            }
        }

        if ($type == 4) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(4000, 4999));
            }
        }

        if ($type == 5) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(5000, 5999));
            }
        }

        if ($type == 6) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(6000, 6999));
            }
        }

        if ($type == 7) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(7000, 7999));
            }
        }

        if ($type == 8) {
            for($a = 0;$a < $num; $a++) {
                array_push($prizeArr, mt_rand(8000, 8999));
            }
        }

        shuffle($prizeArr);
        $longText = implode(",", $prizeArr);

        $setting->where('jack_key', 'prizeArr')->update([
            'jack_value' => $longText
        ]);

        return $this->json_data(1, '000000', '修改成功', '', '');
    }


    public function webhook(Request $request) {

    }


    /**
     *
     *  导出用户信息
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exportUserExcel() {
        $PHPExcel = new \PHPExcel();

        vendor("PHPExcel.PHPExcel"); //方法一
        $PHPSheet = $PHPExcel->getActiveSheet();

        $PHPSheet->setTitle("订单数据"); //给当前活动sheet设置名称

        $PHPSheet->setCellValue("A1", "用户ID");
        $PHPSheet->setCellValue("B1", "微信昵称");
        $PHPSheet->setCellValue("C1", "openid");
        $PHPSheet->setCellValue("D1", "姓名");
        $PHPSheet->setCellValue("E1", "手机号");
        $PHPSheet->setCellValue("F1", "性别");
        $PHPSheet->setCellValue("G1", "抽奖次数");
        $PHPSheet->setCellValue("H1", "是否支付");
        $PHPSheet->setCellValue("I1", "注册时间");


        $user = new UserModel();

        $result = $user->select();

        $i = 2;
        foreach($result as $data){
            $PHPSheet->setCellValue("A" . $i, $data['id'])
                ->setCellValue("B" . $i, $data['nickName'])
                ->setCellValue("C" . $i, $data['openId'])
                ->setCellValue("D" . $i, $data['name'])
                ->setCellValue("E" . $i, $data['phone'])
                ->setCellValue("F" . $i, $data['gender'] == 1 ? '男' : '女')
                ->setCellValue("G" . $i, $data['count'])
                ->setCellValue("H" . $i, $data['is_ticket'])
                ->setCellValue("I" . $i, date('Y-m-d h:i:s', $data['create_time']));
            $i++;
        }

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        header('Content-Disposition: attachment;filename="'. date('Y-m-d h-i-s', time()) .'用户信息.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); // 表示在$path路径下面生成demo.xlsx文件
    }


    /***
     *
     * 导出商家信息
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exportSellerExcel() {
        $PHPExcel = new \PHPExcel();

        vendor("PHPExcel.PHPExcel"); //方法一
        $PHPSheet = $PHPExcel->getActiveSheet();

        $PHPSheet->setTitle("订单数据"); //给当前活动sheet设置名称

        $PHPSheet->setCellValue("A1", "商户ID");
        $PHPSheet->setCellValue("B1", "商户名称");
        $PHPSheet->setCellValue("C1", "省");
        $PHPSheet->setCellValue("D1", "市");
        $PHPSheet->setCellValue("E1", "区");
        $PHPSheet->setCellValue("F1", "详细地址");
        $PHPSheet->setCellValue("G1", "联系人1");
        $PHPSheet->setCellValue("H1", "手机1");
        $PHPSheet->setCellValue("I1", "联系人2");
        $PHPSheet->setCellValue("J1", "手机2");


        $sellerInfo = new SellerInfoModel();

        $result = $sellerInfo->select();

        $i = 2;
        foreach($result as $data){
            $PHPSheet->setCellValue("A" . $i, $data['id'])
                ->setCellValue("B" . $i, $data['company_name'])
                ->setCellValue("C" . $i, $data['province'])
                ->setCellValue("D" . $i, $data['city'])
                ->setCellValue("E" . $i, $data['area'])
                ->setCellValue("F" . $i, $data['address'])
                ->setCellValue("G" . $i, $data['company_person'])
                ->setCellValue("H" . $i, $data['phone2'])
                ->setCellValue("I" . $i, $data['legal_person'])
                ->setCellValue("J" . $i, $data['phone1']);

            $i++;
        }

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        header('Content-Disposition: attachment;filename="'. date('Y-m-d h-i-s', time()) .'商户信息.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); // 表示在$path路径下面生成demo.xlsx文件
    }


    /**
     *
     * 导出核销信息数据
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function exportCheckExcel() {
        $PHPExcel = new \PHPExcel();

        vendor("PHPExcel.PHPExcel"); //方法一
        $PHPSheet = $PHPExcel->getActiveSheet();

        $PHPSheet->setTitle("订单数据"); //给当前活动sheet设置名称

        $PHPSheet->setCellValue("A1", "订单ID");
        $PHPSheet->setCellValue("B1", "核销商户");
        $PHPSheet->setCellValue("C1", "商家电话");
        $PHPSheet->setCellValue("D1", "用户姓名");
        $PHPSheet->setCellValue("E1", "用户手机");
        $PHPSheet->setCellValue("F1", "抵购券");
        $PHPSheet->setCellValue("G1", "核销商品");
        $PHPSheet->setCellValue("H1", "核销代码");
        $PHPSheet->setCellValue("I1", "核销时间");


        $ticketCheck = new TicketCheckModel();
        $seller = new SellerModel();
        $sellerInfo = new SellerInfoModel();
        $user = new UserModel();
        $ticket = new TicketModel();

        $data = $ticketCheck
            ->order('id', 'desc')
            ->select();

        $result = array();

        foreach ($data as $item) {
            $userData = $user->where('id', $item['uid'])->find();
            $ticketData = $ticket->where('id', $item['qid'])->find();
            $phone = $seller->where('id', $item['sid'])->value('uname');
            $sellerData = $sellerInfo->whereOr('phone2', $phone)
                ->whereOr('phone1', $phone)
                ->find();
            $item['user1'] = $userData;
            $item['ticket1'] = $ticketData;
            $item['seller'] = $sellerData;

            array_push($result, $item);
        }

        $i = 2;
        foreach($result as $data){
            $PHPSheet->setCellValue("A" . $i, $data['id'])
                ->setCellValue("B" . $i, $data['seller']['company_name'])
                ->setCellValue("C" . $i, $data['seller']['phone2'])
                ->setCellValue("D" . $i, $data['user1']['name'])
                ->setCellValue("E" . $i, $data['user1']['phone'])
                ->setCellValue("F" . $i, $data['ticket1']['tname'])
                ->setCellValue("G" . $i, $data['goods'])
                ->setCellValue("H" . $i, $data['code'])
                ->setCellValue("I" . $i, date('Y-m-d h:i:s', $data['create_time']));

            $i++;
        }

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        header('Content-Disposition: attachment;filename="'. date('Y-m-d h-i-s', time()) .'核销信息.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); // 表示在$path路径下面生成demo.xlsx文件
    }


    public function exportRecodeExcel() {
        $PHPExcel = new \PHPExcel();

        vendor("PHPExcel.PHPExcel"); //方法一
        $PHPSheet = $PHPExcel->getActiveSheet();

        $PHPSheet->setTitle("中奖数据"); //给当前活动sheet设置名称

        $PHPSheet->setCellValue("A1", "中奖ID");
        $PHPSheet->setCellValue("B1", "用户姓名");
        $PHPSheet->setCellValue("C1", "用户手机号");
        $PHPSheet->setCellValue("D1", "中奖记录");
        $PHPSheet->setCellValue("E1", "中奖时间");

        $recode = new RecodeModel();
        $user = new UserModel();
        $data = $recode->select();

        $result = array();

        foreach ($data as $item) {
            $userData = $user->where('id', $item['uid'])->find();

            $item['user'] = $userData;
            array_push($result, $item);
        }


        $i = 2;
        foreach($result as $data){
            $PHPSheet->setCellValue("A" . $i, $data['id'])
                ->setCellValue("B" . $i, $data['user']['name'])
                ->setCellValue("C" . $i, $data['user']['phone'])
                ->setCellValue("D" . $i, $data['content'])
                ->setCellValue("E" . $i, date('Y-m-d h:i:s', $data['create_time']));
            $i++;
        }

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        header('Content-Disposition: attachment;filename="'. date('Y-m-d h-i-s', time()) .'中奖记录.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); // 表示在$path路径下面生成demo.xlsx文件


    }


    public function exportSellerAccountExcel() {
        $PHPExcel = new \PHPExcel();

        vendor("PHPExcel.PHPExcel"); //方法一
        $PHPSheet = $PHPExcel->getActiveSheet();

        $PHPSheet->setTitle("中奖数据"); //给当前活动sheet设置名称

        $PHPSheet->setCellValue("A1", "经销商ID");
        $PHPSheet->setCellValue("B1", "账号");
        $PHPSheet->setCellValue("C1", "密码");
        $PHPSheet->setCellValue("D1", "商户名称");
        $PHPSheet->setCellValue("E1", "注册地址");
        $PHPSheet->setCellValue("F1", "注册时间");

        $seller = new SellerModel();
        $sellerInfo = new SellerInfoModel();

        $data = $seller->select();

        $result = array();

        foreach ($data as $item) {
            $phone = $seller->where('id', $item['id'])->value('uname');
            $sellerData = $sellerInfo->whereOr('phone2', $phone)
                ->whereOr('phone1', $phone)
                ->find();

            $item['seller'] = $sellerData;
            array_push($result, $item);
        }


        $i = 2;
        foreach($result as $data){
            $PHPSheet->setCellValue("A" . $i, $data['id'])
                ->setCellValue("B" . $i, $data['uname'])
                ->setCellValue("C" . $i, $data['password'])
                ->setCellValue("D" . $i, $data['seller']['company_name'])
                ->setCellValue("E" . $i, $data['seller']['address'])
                ->setCellValue("F" . $i, date('Y-m-d h:i:s', $data['create_time']));
            $i++;
        }

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        header('Content-Disposition: attachment;filename="'. date('Y-m-d h-i-s', time()) .'经销商账号信息.xlsx');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output"); // 表示在$path路径下面生成demo.xlsx文件

    }


    /**
     *
     * 商户信息列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sellerInfoList(Request $request) {

        $page = $request->param('page') ? $request->param('page') : 1;
        $num = $request->param('num') ? $request->param('num') : 10;
        $type = $request->param('type') ? $request->param('type') : 0;
        $keyword = $request->param('keyword') ? $request->param('keyword') : '';

        $settingInfo = new SellerInfoModel();

        $data = null;
        $total = 0;
        if (!empty($keyword)) {
            $data = $settingInfo
                ->whereOr('company_name', 'like', '%'.$keyword.'%')
                ->whereOr('company_person', 'like', '%'.$keyword.'%')
                ->whereOr('phone2', 'like', '%'.$keyword.'%')
                ->whereOr('legal_person', 'like', '%'.$keyword.'%')
                ->whereOr('phone1', 'like', '%'.$keyword.'%')
                ->limit(($page - 1) * $num, $num)
                ->order('id', 'desc')
                ->select();

            $total = count($settingInfo
                ->whereOr('company_name', 'like', '%'.$keyword.'%')
                ->whereOr('company_person', 'like', '%'.$keyword.'%')
                ->whereOr('phone2', 'like', '%'.$keyword.'%')
                ->whereOr('legal_person', 'like', '%'.$keyword.'%')
                ->whereOr('phone1', 'like', '%'.$keyword.'%')
                ->select());
        } else {
            $data = $settingInfo->limit(($page - 1) * $num, $num)
                ->order('id', 'desc')
                ->select();

            $total = count($settingInfo->select());
        }

        return $this->json_data(1, '000000', '商户信息表', [
            'total' => $total,
            'data' => $data
        ], '');
    }


//    /**
//     * @return array
//     */
//    public function searchUser(Request $request)
//    {
//        $keyword = $request->param('keyword');
//
//        $user = new UserModel();
//
//        $data = $user->whereLike('nickName', $keyword)->select();
//
//
//
//    }

    /***
     *
     * 添加商户信息
     * @param Request $request
     * @return \think\response\Json
     */
    public function addSellerInfo(Request $request) {

        $data = [
            'company_name' => $request->param('company_name'),
            'province' => $request->param('province'),
            'city' => $request->param('city'),
            'area' => $request->param('area'),
            'address' => $request->param('address'),
            'company_person' => $request->param('company_person'),
            'phone2' => $request->param('phone2'),
            'legal_person' => $request->param('legal_person'),
            'phone1' => $request->param('phone1'),
        ];

        $sellerInfo = new SellerInfoModel();

        $sellerInfo->data($data)->save();

        return  $this->json_data(1, '000000', '添加活动信息', '', '');

    }


    /**
     *
     * 编辑商户信息
     * @param Request $request
     *
     */
    public function editSellerInfo(Request $request) {

        $sid = $request->param('sid');

        $data = [
            'company_name' => $request->param('company_name'),
            'province' => $request->param('province'),
            'city' => $request->param('city'),
            'area' => $request->param('area'),
            'address' => $request->param('address'),
            'company_person' => $request->param('company_person'),
            'phone2' => $request->param('phone2'),
            'legal_person' => $request->param('legal_person'),
            'phone1' => $request->param('phone1'),
            'update_time' => time()
        ];

        $sellerInfo = new SellerInfoModel();

        $data = $sellerInfo->where('id', $sid)->update($data);

        return  $this->json_data(1, '000000', '编辑成功', $data, '');

    }


    public function getSellerInfo(Request $request) {
        $id = $request->param('sid');

        $sellerInfo = new SellerInfoModel();

        $data = $sellerInfo->where('id', $id)->find();

        return  $this->json_data(1, '000000', '商家信息', $data, '');
    }


    /**
     *
     * 删除商户
     * @param Request $request
     * @return \think\response\Json
     */
    public function delSellerInfo(Request $request) {
        $id = $request->param('sid');

        $sellerInfo = new SellerInfoModel();

        $sellerInfo->where('id', $id)->delete();

        return  $this->json_data(1, '000000', '删除成功', '', '');
    }


    /**
     *
     *  商户账号信息接口
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sellerList(Request $request) {

        $page = $request->param('page') ? $request->param('page') : 1;
        $num = $request->param('num') ? $request->param('num') : 10;

        $keyword = $request->param('keyword') ? $request->param('keyword') : '';


        $seller = new SellerModel();
        $sellerInfo = new SellerInfoModel();

        $data = null;
        $total = 0;

        if (!empty($keyword)) {
            $data = $seller
                ->where('uname', 'like', $keyword)
                ->limit(($page - 1) * $num, $num)
                ->order('id', 'desc')
                ->select();

            $total = count($seller->where('uname', 'like', $keyword)->select());
        } else {
            $data = $seller
                ->limit(($page - 1) * $num, $num)
                ->order('id', 'desc')
                ->select();

            $total = count($seller->select());
        }

        $result = array();

        foreach ($data as $item) {
            $sellerData = $sellerInfo
                ->whereOr('phone1', $item['uname'])
                ->whereOr('phone2', $item['uname'])->find();
            $item['seller'] = $sellerData;

            array_push($result, $item);
        }

        return $this->json_data(1, '000000', '商家记录', [
            'total' => $total,
            'data' => $result
        ], '');


    }


    public function recodeList(Request $request) {

        $page = $request->param('page') ? $request->param('page') : 1;
        $num = $request->param('num') ? $request->param('num') : 10;

        $keyword = $request->param('keyword') ? $request->param('keyword') : '';

        $recode = new RecodeModel();
        $user = new UserModel();

        $data = $recode
            ->limit(($page - 1) * $num, $num)
            ->order('id', 'desc')
            ->select();

        $result = array();

        foreach ($data as $item) {
            $userData = $user
                ->where('id', $item['uid'])->find();
            $item['user'] = $userData;

            array_push($result, $item);
        }

        return $this->json_data(1, '000000', '商家记录', [
            'total' => count($recode->select()),
            'data' => $result
        ], '');


    }
































}